/**
 * @(#)ParamSelectTag.java 2011-10-13
 * 
 * Copyright 2000-2011 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */

package com.szf.base.web.tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.szf.util.ParamServiceUtil;
import com.szf.util.StringUtil;
import com.szf.util.VelocityServiceUtil;

/**
 * 数据字典下拉菜单的标签
 * 
 * @author Howard.Li
 * @author 刘圳
 * @version 1.1 2011-10-13
 * @since 1.0
 */
public class ParamSelectTag extends BodyTagSupport {

	private static final long serialVersionUID = -471763176135535343L;
	protected Logger logger = LoggerFactory.getLogger(getClass());

	private String id;
	private String name;
	private String paramType;
	private String style;
	private String value;
	private boolean blank;
	private String onChange;
	private String className;

	@Override
	public int doStartTag() throws JspException {
		Map<String, String> paramValue = ParamServiceUtil.getComMap(paramType);

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		param.put("name", name);
		param.put("params", paramValue);
		param.put("style", style);
		param.put("value", value);
		param.put("blank", blank);
		if (StringUtil.isEmpty(onChange))
			param.put("cgm", "");
		else
			param.put("cgm", onChange);
		if (StringUtil.isEmpty(className))
			param.put("clsname", "");
		else
			param.put("clsname", className);

		this.parseVelocity("paramSelect.vm", param);

		return super.doStartTag();
	}

	/**
	 * @return the id
	 */
	@Override
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	@Override
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the paramType
	 */
	public String getParamType() {
		return paramType;
	}

	/**
	 * @param paramType
	 *            the paramType to set
	 */
	public void setParamType(String paramType) {
		this.paramType = paramType;
	}

	/**
	 * @return the style
	 */
	public String getStyle() {
		return style;
	}

	/**
	 * @param style
	 *            the style to set
	 */
	public void setStyle(String style) {
		this.style = style;
	}

	/**
	 * @return the value
	 */
	public String getValue() {
		return value;
	}

	/**
	 * @param value
	 *            the value to set
	 */
	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * @return the blank
	 */
	public boolean isBlank() {
		return blank;
	}

	/**
	 * @param blank
	 *            the blank to set
	 */
	public void setBlank(boolean blank) {
		this.blank = blank;
	}

	/**
	 * @return the onChange
	 */
	public String getOnChange() {
		return onChange;
	}

	/**
	 * @param onChange
	 *            the onChange to set
	 */
	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}

	/**
	 * @return the className
	 */
	public String getClassName() {
		return className;
	}

	/**
	 * @param className
	 *            the className to set
	 */
	public void setClassName(String className) {
		this.className = className;
	}

	protected void writeContent(String content) {
		try {
			this.pageContext.getOut().write(content);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}

	protected void parseVelocity(String templateLocation, Map model) {
		if (model == null) {
			model = new HashMap();
		}
		String content = VelocityServiceUtil.mergeTemplateIntoString(templateLocation, model);

		writeContent(content);
	}
}
